import { EAreaID, ELogChangeID, ELogFromID, EPokerState } from "../constant/GameEnum";
import { GameEvent } from "../constant/GameEvent";
import CreateVoUtils from "../utils/CreateVoUtils";
import GameCtrl from "./GameCtrl";
import LogPokerVo from "./LogPokerVo";
import ModelGame from "./ModelGame";
import PokerGroupVo from "./PokerGroupVo";
import PokerVo from "./PokerVo";

export default class PlayPokerGroupVo extends PokerGroupVo {
  private _groupIndex: number = -1;
  constructor(areaID: EAreaID = EAreaID.PLAY) {
    super(areaID);
  }
  public get groupIndex(): number {
    return this._groupIndex;
  }
  public set groupIndex(v: number) {
    this._groupIndex = v;
  }
  /**
   *覆写这个方法
   *有删除动作的时候检测是否改变顶部poker的状态
   */
  public delTopPoker(num: number = 1): void {
    super.delTopPoker(num);
    if (this.pokers.length !== 0 && this.topPoker.state !== EPokerState.OPEN) {
      let poker: PokerVo = this.topPoker;
      poker.state = EPokerState.OPEN;
      if (GameCtrl.ins.logPoker) {
        ModelGame.ins.emit(GameEvent.CHANGE_PLAY_POKER_STATE, poker);
        let vo: LogPokerVo = CreateVoUtils.createLogPoker(ELogFromID.POKER_CHANGE_STAGE, [poker], ELogChangeID.Flop);
        GameCtrl.ins.addLogPoker(vo);
      }
    }
  }
  public isNextPokerByTopPoker(poker: PokerVo): boolean {
    if (!this.havePoker) {
      return poker.point === 13;
    } else {
      if (this.topPoker.point == poker.point + 1 && (poker.suit + this.topPoker.suit) % 2 != 0) {
        return true;
      }
    }
    return false;
  }
  public getConnectPoker(poker: PokerVo): PokerVo[] {
    let pokers: PokerVo[] = [];
    pokers.push(poker);
    let index = this.getIndexByGroup(poker);
    for (let i = index; i < this.Length - 1; i++) {
      let pokerVo: PokerVo = this.getPokerByIndex(i);
      let nextPoker: PokerVo = this.getPokerByIndex(i + 1);
      if (nextPoker.point + 1 == pokerVo.point && (nextPoker.suit + pokerVo.suit) % 2 != 0) {
        pokers.push(nextPoker);
      } else {
        break;
      }
    }
    return pokers;
  }
}
